package net.zjitc.dao.impl;

import net.zjitc.dao.AdminCateGorySecondDao;
import net.zjitc.pojp.CateGory;
import net.zjitc.pojp.CateGorySecond;
import net.zjitc.utils.db.C3P0Utils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2017/11/15.
 */
public class AdminCateGorySecondDaoImpl implements AdminCateGorySecondDao {
    public List<CateGorySecond> findAll() {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="select *from categorysecond";
        try {
            List<CateGorySecond> list= (List<CateGorySecond>) runner.query(sql,new BeanListHandler(CateGorySecond.class));
        return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public int insert(CateGorySecond cateGorySecond) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="insert into categorysecond (csname,cid) value(?,?)";
        try {
            int count=runner.update(sql,cateGorySecond.getCsname(),cateGorySecond.getCateGory().getCid());
         return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public CateGorySecond findByCSid(String csid) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="select *from categorysecond where csid=?";
        try {
            /*Map将字段名和对应的值封装成一个map集合，
            csid  1
            csname 羽绒服
            cid   1
            这样使用BeanUtils可以将不同的字段赋值给不同的类*/

            Map<String,Object> map=runner.query(sql, new MapHandler(),csid);
            CateGory cateGory=new CateGory();
            CateGorySecond cateGorySecond=new CateGorySecond();

            BeanUtils.populate(cateGory,map);
            BeanUtils.populate(cateGorySecond,map);

            cateGorySecond.setCateGory(cateGory);
            return cateGorySecond;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public int update(CateGorySecond cateGorySecond) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="update categorysecond set csname=?,cid=? where csid=?";
        try {
            int count=runner.update(sql,cateGorySecond.getCsname(),cateGorySecond.getCateGory().getCid(),cateGorySecond.getCsid());
          return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public List<CateGorySecond> findByCid(int cid) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="select * from categorysecond where cid=?";
        try {
            List<CateGorySecond> cslist= (List<CateGorySecond>) runner.query(sql,new BeanListHandler(CateGorySecond.class),cid);
            return cslist;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
